// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.prefs;

import org.dengues.core.i18n.Messages;
import org.dengues.core.prefs.IDenguesPrefsConstant;
import org.dengues.ui.DenguesUiPlugin;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.DirectoryFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-6-22 qiang.zhang $
 * 
 */
public class DenguesRuntimePreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {

    private Group clientGroup;

    private IntegerFieldEditor integerFieldEditor;

    private IntegerFieldEditor socketPort;

    /**
     * Qiang.Zhang.Adolf@gmail.com DenguesRuntimePreferencePage constructor comment.
     */
    public DenguesRuntimePreferencePage() {
        setPreferenceStore(DenguesUiPlugin.getDefault().getPreferenceStore());
    }

    public void init(IWorkbench workbench) {

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.preference.FieldEditorPreferencePage#createFieldEditors()
     */
    @Override
    protected void createFieldEditors() {
        Composite fieldEditor = getFieldEditorParent();
        fieldEditor.setLayout(new GridLayout());
        Composite parent = new Composite(fieldEditor, SWT.NONE);
        GridLayout gridLayout = new GridLayout(1, false);
        gridLayout.marginBottom = 0;
        gridLayout.marginWidth = 0;
        gridLayout.marginTop = 0;
        gridLayout.marginRight = 0;
        gridLayout.marginLeft = 0;
        gridLayout.marginHeight = 0;
        parent.setLayout(gridLayout);
        GridData gridData = new GridData(GridData.FILL_BOTH);
        parent.setLayoutData(gridData);
        BooleanFieldEditor isTracField = new BooleanFieldEditor(IDenguesPrefsConstant.SOCKET_ENABLED,
                org.dengues.ui.i18n.Messages.getString("DenguesConnectivityPreferencePage.tracEnable"), parent) {//$NON-NLS-1$

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.jface.preference.BooleanFieldEditor#valueChanged(boolean, boolean)
             */
            @Override
            protected void valueChanged(boolean oldValue, boolean newValue) {
                super.valueChanged(oldValue, newValue);
                integerFieldEditor.setEnabled(newValue, clientGroup);
                socketPort.setEnabled(newValue, clientGroup);
            }
        };
        addField(isTracField);

        clientGroup = new Group(parent, SWT.NONE);
        clientGroup.setText(org.dengues.ui.i18n.Messages.getString("DenguesPreferencePage.StatConfig")); //$NON-NLS-1$
        GridLayout layout = new GridLayout();
        layout.numColumns = 4;
        clientGroup.setLayout(layout);
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        clientGroup.setLayoutData(gd);

        integerFieldEditor = new IntegerFieldEditor(IDenguesPrefsConstant.SOCKET_TIME_SPACING, Messages
                .getString("DenguesPreferencePage.timeSpacing"), //$NON-NLS-1$
                clientGroup);
        addField(integerFieldEditor);

        socketPort = new IntegerFieldEditor(IDenguesPrefsConstant.SOCKET_PORT, Messages
                .getString("DenguesPreferencePage.socket.port"), //$NON-NLS-1$
                clientGroup);
        addField(socketPort);

        boolean newValue = getPreferenceStore().getBoolean(IDenguesPrefsConstant.SOCKET_ENABLED);
        integerFieldEditor.setEnabled(newValue, clientGroup);
        socketPort.setEnabled(newValue, clientGroup);

        Composite composite = new Composite(parent, SWT.NONE);
        gridLayout = new GridLayout(1, false);
        gridLayout.marginWidth = 0;
        gridLayout.marginHeight = 0;
        composite.setLayout(gridLayout);
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        composite.setLayoutData(gridData);

        DirectoryFieldEditor directoryFieldEditor = new DirectoryFieldEditor(IDenguesPrefsConstant.SERVER_DIRECTORY,
                org.dengues.ui.i18n.Messages.getString("DenguesPreferencePage.server.label"), composite); //$NON-NLS-1$
        directoryFieldEditor.setChangeButtonText(org.dengues.ui.i18n.Messages.getString("DenguesPreferencePage.server.btnLabel")); //$NON-NLS-1$
        addField(directoryFieldEditor);

        Group logGroup = new Group(parent, SWT.NONE);
        logGroup.setText(org.dengues.ui.i18n.Messages.getString("DenguesPreferencePage.sysLog.config"));
        layout = new GridLayout(1, false);
        logGroup.setLayout(layout);
        gd = new GridData(GridData.FILL_HORIZONTAL);
        logGroup.setLayoutData(gd);

        composite = new Composite(logGroup, SWT.NONE);
        gridLayout = new GridLayout(1, false);
        gridLayout.marginWidth = 0;
        gridLayout.marginHeight = 0;
        composite.setLayout(gridLayout);
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        composite.setLayoutData(gridData);
        DirectoryFieldEditor logDirFieldEditor = new DirectoryFieldEditor(IDenguesPrefsConstant.SYSTEM_LOG_DIRECTORY,
                org.dengues.ui.i18n.Messages.getString("DenguesPreferencePage.sysLog.label"), composite); //$NON-NLS-1$
        logDirFieldEditor.setChangeButtonText(org.dengues.ui.i18n.Messages.getString("DenguesPreferencePage.server.btnLabel")); //$NON-NLS-1$
        addField(logDirFieldEditor);

        composite = new Composite(logGroup, SWT.NONE);
        gridLayout = new GridLayout(1, false);
        gridLayout.marginWidth = 0;
        gridLayout.marginHeight = 0;
        composite.setLayout(gridLayout);
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        composite.setLayoutData(gridData);
        StringFieldEditor logFileEditor = new StringFieldEditor(IDenguesPrefsConstant.SYSTEM_LOG_FILENAME,
                org.dengues.ui.i18n.Messages.getString("DenguesPreferencePage.sysLog.fileName"), composite);
        addField(logFileEditor);
    }

}
